//
//  weChatEngine.m
//  test1
//
//  Created by gengzhangjia on 15/7/30.
//  Copyright (c) 2015年 gengzhangjia. All rights reserved.
//

#import "weChatEngine.h"
#import "WXApi.h"
#import "weChatResult.h"
#import "weChatAuthResult.h"
#import "weChatContentResult.h"

@interface weChatEngine () <WXApiDelegate>
{
    /**
     *  微信AppID
     */
    NSString *_weChatAppID;
}
@property (nonatomic, strong) weChatCallBackBlock weChatCallBackBlock;
@end

@implementation weChatEngine

+(instancetype)shareWeChatEngine
{
    static weChatEngine *weChatEngine =nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        weChatEngine =[[[self class] alloc] init];
    });
    return weChatEngine;
}

- (instancetype) init
{
    if(self = [super init])
    {
        _weChatAppID = [self readWeChatAppID];
        [WXApi registerApp:_weChatAppID];
    }
    return self;
}

- (BOOL) canHandleOpenURL:(NSURL *) URL
{
    BOOL ret = [URL.scheme isEqualToString:_weChatAppID];
    return ret;
}

- (BOOL) handleOpenURL:(NSURL *) URL
{
    BOOL ret =NO;
    if([self canHandleOpenURL:URL])
    {
        ret =[WXApi handleOpenURL:URL delegate:self];
    }
    return  ret;
}

- (NSString *) getInstallUrl
{
    return [WXApi getWXAppInstallUrl];
}

- (NSString *) getSDKVersion
{
    return [WXApi getApiVersion];
}

- (BOOL) openApp
{
    return [WXApi openWXApp];
}

- (BOOL) isInstallWX
{
    return [WXApi isWXAppInstalled];
}

- (BOOL) isAppSupportAPI
{
    return [WXApi isWXAppSupportApi];
}

- (NSString *) readWeChatAppID
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSArray *array = [infoDic objectForKey:@"CFBundleURLTypes"];
    
    for (NSDictionary *dic in array) {
        NSString *name = [dic objectForKey:@"CFBundleURLName"];
        if (name && name.length > 0 && [name isEqualToString:@"weixin"]) {
            NSArray *weixinArray = [dic objectForKey:@"CFBundleURLSchemes"];
            if (weixinArray && weixinArray.count > 0) {
                _weChatAppID = [weixinArray objectAtIndex:0];
                break;
            }
        }
    }
    return _weChatAppID;
}

- (void) sendReqWithObject:(weChatBaseObject *) baseObject
             callBackBlock:(weChatCallBackBlock) callBackBlock
{
    if(!baseObject ||![baseObject isKindOfClass:[weChatBaseObject class]])
    {
        return;
    }
    
    weChatResult *result =nil;
    if(![self isInstallWX])
    {
        result =[weChatResult notInstallApp];
        if(callBackBlock)
        {
            callBackBlock(result);
        }
        return;
    }
    
    if(![self isAppSupportAPI])
    {
        result =[weChatResult notSupportAPI];
        if(callBackBlock)
        {
            callBackBlock(result);
        }
        return ;
    }
    
    result =[baseObject verifyData];
    if(result)
    {
        if(callBackBlock)
        {
            callBackBlock(result);
        }
        return;
    }
    
    BaseReq *req =[baseObject wxMessage];
    if(!req)
    {
        result =[weChatResult notError];
    }
    
    if(!result)
    {
        if(![WXApi sendReq:req])
        {
            result =[weChatResult notError];
        }
    }
    
    if(result)
    {
        if(callBackBlock)
        {
            callBackBlock(result);
        }
        return;
    }
    _weChatCallBackBlock =callBackBlock;
}

#pragma mark WXApiDelegate
-(void) onResp:(BaseResp*)resp
{
    weChatResult *result =nil;
    if([resp isKindOfClass:[SendAuthResp class]])
    {
        result =[[weChatAuthResult alloc] init];
    }
    else if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        result =[[weChatContentResult alloc] init];
    }
    if(result)
    {
        [result parseResult:resp];
    }
    
    _weChatCallBackBlock(result);
}
@end
